package com.shakespace.artofandroid.chapter07anim

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.util.Log
import android.view.animation.*
import androidx.appcompat.app.AppCompatActivity
import com.shakespace.artofandroid.R
import com.shakespace.firstlinecode.global.TAG
import kotlinx.android.synthetic.main.activity_animation.*


class AnimationActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animation)

        val alpha = AlphaAnimation(1f, 0f)
        alpha.duration = 2000
        alpha.repeatCount = -1
        alpha.repeatMode = Animation.REVERSE

        // 以自身中心为参照
        val rotate = RotateAnimation(
            -90f,
            270f,
            RotateAnimation.RELATIVE_TO_SELF,
            0.5f,
            RotateAnimation.RELATIVE_TO_SELF,
            0.5f
        ).apply {
            duration = 1000
            repeatCount = -1
            repeatMode = Animation.REVERSE
        }

        // 以自身中心为参照
        val scale = ScaleAnimation(
            0.5f,
            2f,
            0.2f,
            3f,
            ScaleAnimation.RELATIVE_TO_SELF,
            0.5f,
            ScaleAnimation.RELATIVE_TO_SELF,
            0.5f
        ).apply {
            duration = 1000
            repeatCount = -1
            repeatMode = Animation.REVERSE
        }

        val trans = TranslateAnimation(
            TranslateAnimation.RELATIVE_TO_SELF,
            0f,
            TranslateAnimation.RELATIVE_TO_SELF,
            3f,
            TranslateAnimation.RELATIVE_TO_SELF,
            0f,
            TranslateAnimation.RELATIVE_TO_SELF,
            3f
        ).apply {
            duration = 1000
            repeatCount = -1
            repeatMode = Animation.REVERSE
        }

        tv_alpha.setOnClickListener {
            iv_anim.startAnimation(alpha)
        }

        tv_rotate.setOnClickListener {
            iv_anim.startAnimation(rotate)
        }

        tv_scale.setOnClickListener {
            iv_anim.startAnimation(scale)
        }

        tv_translation.setOnClickListener {
            iv_anim.startAnimation(trans)
        }

        tv_complex.setOnClickListener {
            val set = AnimationSet(false)
            set.addAnimation(alpha)
            set.addAnimation(rotate)
            set.addAnimation(scale)
            set.addAnimation(trans)

            set.duration = 3000
            iv_anim.startAnimation(set)

        }

        // 动画监听
        trans.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
                Log.e(this.TAG, "onAnimationRepeat:  ")
            }

            override fun onAnimationEnd(animation: Animation?) {
                Log.e(this.TAG, "onAnimationEnd:  ")
            }

            override fun onAnimationStart(animation: Animation?) {
                Log.e(this.TAG, "onAnimationStart:  ")
            }

        })

        // 使用xml 中定义的动画
        tv_xml.setOnClickListener {
            iv_anim.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha))
        }

        tv_frame.setOnClickListener {
            val animationDrawable = tv_frame.background as AnimationDrawable
            animationDrawable.start()
        }



    }

    override fun onBackPressed() {
        super.onBackPressed()
        overridePendingTransition(R.anim.act_back_enter, R.anim.act_back_exit)
    }
}
